/**
 * 原型表达式div
 */
angular.module('app').component('winitTransform', {
    templateUrl: getTemplateUrl('matrix/component/tpl/winitTransform.html'),
    bindings: {},
    controller: function ($scope, $rootScope, $interval, lodash, $filter, $timeout) {
        var ctrl = this;
        $scope.info = {
            expression: "",
            tableContent: "table\n[],出口报关指令单号|l,出口报关服务单号|l|u,物流单号\n[],CK01000184879,D010101000040946,C030101000027326\n[], CK01000184879,D010101000040946,C030101000027326\nEnd\n",
            tabTableContent: "tab-table\n全部,等待清关|v,清关完成--创建\n[],出口报关指令单号|l,出口报关服务单号|l|u,物流单号\n[],CK01000184879,D010101000040946,C030101000027326,\n[],CK01000184879,D010101000040946,C030101000027326\nEnd\n",
            formContent: "form\ncombo-select:光建筑,派送指令单/xxx指令单|u\nselect:是否异常,请选择/是/否\ninput:客户\ndate:日期*;必须输入,日期小于当天\ndate-to: 起止\ncustomer:某控件,请参考原型\nsearch-button:查询\nEnd\n",
            textFormContent: "text-form\nname*,文本框;info1/info2/info3|u\nname*,文本框;info1/info2/info3\nname*,文本框;info1/info2/info3\nEnd\n",
            flowContent: "flow\n1111|u\n222\n333\nEnd\n",
            selectedItem: "default",
            isEdit: false,
            expressionData: null
        };
        //保存原型表达式
        $scope.saveExpression = function () {
            $scope.info.selectedItem = "default";
            if ($scope.info.expression == "") {
                $rootScope.toasterErrorInfo("数据不能为空！");
                return false;
            }
            $scope.info.isEdit = false;

            var splitByEnd = $scope.info.expression.split("End\n");
            var resultArray = [];
            angular.forEach(splitByEnd, function (trans, index) {
                var currentType = lodash.trim(trans.split("\n")[0]);
                if (currentType != "tab-table" && currentType != "form" && currentType != "text-form" && currentType != "flow" && currentType != "table") {
                    currentType = "other";
                }
                resultArray.push({"type": currentType, "data": trans});
            });
            $scope.info.expressionData = resultArray;
        };

        //编辑原型表达式
        $scope.editExpression = function () {
            $scope.info.selectedItem = "default";
            $scope.info.isEdit = true;
        };

        $scope.cancelExpression = function () {
            $scope.info.selectedItem = "default";
            $scope.info.isEdit = false;
        };

        $scope.clearExpression = function () {
            $scope.info.selectedItem = "default";
            $scope.info.expression = "";
        };
        $scope.sltExpression = function () {
            var content = "";
            switch ($scope.info.selectedItem) {
                case "table":
                    content = $scope.info.tableContent;
                    break;
                case "tab-table":
                    content = $scope.info.tabTableContent;
                    break;
                case "form":
                    content = $scope.info.formContent;
                    break;
                case "text-form":
                    content = $scope.info.textFormContent;
                    break;
                case "flow":
                    content = $scope.info.flowContent;
                    break;
            }
            if (content != "") {
                if($scope.info.expression != ''){
                    if ($scope.info.expression.endsWith("\nEnd")) {
                        $scope.info.expression += "\n";
                    }
                    if (!$scope.info.expression.endsWith("\nEnd\n")) {
                        $scope.info.expression += "\nEnd\n";
                    }
                }
                $scope.info.expression += content;
            }
        }
    }

})